package com.demo.webapp.ui.displayer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.demo.webapp.ui.Component;
import com.demo.webapp.ui.ComponentBase;
import com.demo.webapp.ui.DisplayerConfig;

/**
 * 展现Map的实现类
 * 
 * @author ryuu.kk
 *
 */
public class DisplayerMappingImpl implements DisplayerMapping {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = -7631143588775500087L;
	/**
	 * Log日志
	 */
    protected final transient Log log = LogFactory.getLog(getClass());
	/**
	 * Displayer的存储Map
	 */
	private Map<String, Displayer<? extends Component>> mapping = new java.util.concurrent.ConcurrentHashMap<String, Displayer<? extends Component>>();
	
	private List<DisplayerConfig> displayerConfig = new ArrayList<DisplayerConfig>();
	@Override
	public boolean support(ComponentBase model) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public Displayer<? extends Component> get(String name) {
		if (!mapping.containsKey(name)) {
			log.warn(name + " already inexistence!");
			return null;
		}
		return mapping.get(name);
	}
	
	@Override
	public Collection<Displayer<? extends Component>> getAll() {
		return mapping.values();
	}
	
	/**
	 * 为继承类提供带有Name参数的put方法
	 * @param name 显示对象名称
	 * @param displayer 显示对象
	 */
	protected void put(String name, Displayer<? extends Component> displayer) {
		// 
		String disName = name;
		if (displayer == null) {
			log.warn("the displayer " + disName + " is null.");
			return;
		}
		if (mapping.containsKey(disName)) {
			log.warn(disName + " already existed, rewrite it!");
		}
		mapping.put(disName, displayer);
	}
	
	@Override
	public void put(Displayer<? extends Component> displayer) {
		if (displayer != null) {
			put(displayer.getName(), displayer);
		}
	}
	@Override
	public boolean containsKey(String key) {
		if (mapping == null) {
			return false;
		}
		return mapping.containsKey(key);
	}

	@Override
	public List<DisplayerConfig> getDisplayerConfig() {
		return displayerConfig;
	}

	@Override
	public void addDisplayerConfig(DisplayerConfig displayerConfig) {
		this.displayerConfig.add(displayerConfig);
	}
}